package com.shadow.study.test.线程;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
public class DoSomeThingService {
    private final Random random = new Random();
    /* 随机范围 */
    private static final int BOUND = 5000;

    @Async("dThreadPool")
    public void doSomeThing(Long handlerId) {
        try {
            int sleep = random.nextInt(BOUND);
            log.info("sleep : {}", sleep);
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            log.error("sleep error", e);
            Thread.currentThread().interrupt();
        }
        log.info("doSomeThing. handlerId : {}", handlerId);
    }

    @Async("dThreadPool")
    public CompletableFuture<String> doSomeThing1(Long handlerId) {
        try {
            int sleep = random.nextInt(BOUND);
            log.info("sleep : {}", sleep);
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            log.error("sleep error", e);
            Thread.currentThread().interrupt();
        }
        log.info("doSomeThing. handlerId : {}", handlerId);
        return CompletableFuture.completedFuture(String.format("handlerId[%s] success", handlerId));
    }
}
